//
//  Categories+Categories.m
//  ClinicalCollections
//
//  Created by janakiraman gopinath on 2/10/13.
//  Copyright (c) 2013 GOPI. All rights reserved.
//

#import "Categories+Categories.h"

@implementation Categories (Categories)

+ (void) categories:(NSDictionary *) category
           withType:(NSString *) type
       usingMeasure:(Measure *) measure
usingClinicalTrialGov:(ClinicalTrialGov *) clinicalTrialGov
       usingContext:(NSManagedObjectContext *) context
{
    Categories  *categories;
    categories = (Categories *)[NSEntityDescription insertNewObjectForEntityForName:@"Categories"
                                                             inManagedObjectContext:context];

    categories.measure = measure;
    
    
    NSArray *values;
    categories.title = [category valueForKey:@"super_title"];
    
    if ([type isEqual:@"baseline"] || [type isEqual:@"outcome_list"]) {
        if (![[category valueForKey:@"sub_title"] isEqual:[NSNull null]])
            categories.subtitle = [category valueForKey:@"sub_title"];
        if (![[category valueForKey:@"title"] isEqual:[NSNull null]])
            categories.title = [category valueForKey:@"title"];
        if (![[category valueForKey:@"description"] isEqual:[NSNull null]]) {
            categories.desc = [category valueForKey:@"description"];
        }
        values = [category valueForKeyPath:@"measurement_list.measurement"];
    }
    else if([type isEqual:@"participant_flow"]) {
        if (![[category valueForKey:@"title"] isEqual:[NSNull null]])
            categories.subtitle = [category valueForKey:@"title"];
        values = [category valueForKeyPath:@"participants_list.participants"];
    }
    //NSLog(@"Category inserted %@", categories);
    for (int i = 0; i < values.count; i++)
        [[Values class] values:values[i]
                      withType:type
                 usingCategory:categories
         usingClinicalTrialGov:clinicalTrialGov
                  usingContext:context];
   
}
@end
